import { Toast } from '@nutui/nutui'

export const getAssetsFile = (url: string) => {
  return new URL(`../assets/${url}`, import.meta.url).href
}

// 阻止页面滚动
export const stopMove = () => {
  var mo = function (e: any) {
    e.preventDefault()
  }
  document.body.style.overflow = 'hidden'
  document.addEventListener('touchmove', mo, false) // 禁止页面滑动
}
// 允许页面滚动
export const startMove = () => {
  var mo = function (e: any) {
    e.preventDefault()
  }
  document.body.style.overflow = '' // 出现滚动条
  document.removeEventListener('touchmove', mo, false)
}

type Res = {
  code:number,
  message:string,
  result:any
}
export const isRes = (res:any)=>{
  if(res.code == 0){
    return true
  }else{
    Toast.fail(res.message)
  }
}

export const changeGroupBuyStatus = (status:number)=>{
  if(status == -1){
    return '未开始'
  }else if(status ==1){
    return '进行中'
  }else if(status ==0){
    return '已结束'
  }
}

export const changeOrderStatus = (status:number)=>{
  if(status == -1){
    return '拒绝退款'
  }else if(status ==0){
    return '审核中'
  }else if(status ==1){
    return '退款成功'
  }
}

export const changeOrderContentStatus = (status_type:number,status:number)=>{
  if(status ==1){
    if(status_type ==0){
      return '待发货'
    }else if(status_type ==1){
      return '已发货'
    }else if(status_type ==2){
      return '已提货'
    }else if(status_type ==3){
      return '已收货'
    }else if(status_type ==4){
      return '待提货'
    }
  }else if(status == -1){
    return '已关闭'
  }else if(status == 90){
    return '待退款'
  }else if(status == 98){
    return '已取消'
  }else if(status == 99){
    return '已退款'
  }else if(status == 100){
    return '拒绝退款'
  }
}

export const unsignFloat = ( number_only:any) =>{
   //先把非数字的都替换掉，除了数字和小数点
   number_only = number_only.replace(/[^\d.]/g, "");
   //必须保证第一个为数字而不是小数点
   number_only = number_only.replace(/^\./g, "");
   //保证只有出现一个小数点而没有多个小数点
   number_only = number_only.replace(/\.{2,}/g, ".");
   //保证小数点只出现一次，而不能出现两次以上
   number_only = number_only.replace(".","$#$").replace(/\./g, "").replace("$#$", ".");
     //保证只能输入两个小数  
   number_only = number_only.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');
   return number_only;
}


export const resetSetItem = function (key:any, newVal:any) {
  if (key === 'pay_status') {
      // 创建一个StorageEvent事件
      var newStorageEvent = document.createEvent('StorageEvent');
      
      const storage = {
          setItem: function (k:any, val:any) {
              sessionStorage.setItem(k, val);

              // 初始化创建的事件
              newStorageEvent.initStorageEvent('setItem', false, false, k, null,val, undefined  ,null);

              // 派发对象
              window.dispatchEvent(newStorageEvent)
          }
      }
      return storage.setItem(key, newVal);
  }
}